package com.yunc.modules.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.CommonController;
import com.yunc.common.utils.R;
import com.yunc.modules.cms.entity.Question;
import com.yunc.modules.cms.entity.QuestionSubject;
import com.yunc.modules.cms.entity.QuestionSubjectSelect;
import com.yunc.modules.cms.service.IQuestionService;
import com.yunc.modules.cms.service.IQuestionSubjectSelectService;
import com.yunc.modules.cms.service.IQuestionSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 问卷管理表 前端控制器
 *
 * @author concong tao
 * @since 2019-01-31
 */
@RestController
@RequestMapping("/cms/question")
public class QuestionController extends CommonController {

    @Autowired
    private IQuestionService questionService;
    @Autowired
    private IQuestionSubjectService questionSubjectService;
    @Autowired
    private IQuestionSubjectSelectService questionSubjectSelectService;

    @GetMapping("/page")
    public R page(Page<Question> page, Question question){
        questionService.page(page, new QueryWrapper<>(question));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable Long id){
        Question question = questionService.getById(id);
        // 需要获取题目
        if (question == null) {
            return R.error("未查询到问卷");
        }
        QueryWrapper<QuestionSubject> queryWrapper = new QueryWrapper<QuestionSubject>().eq("question_id", question.getId());
        List<QuestionSubject> subjectList = questionSubjectService.list(queryWrapper);
        if (subjectList != null && subjectList.size() > 0) {
            for (QuestionSubject subject : subjectList) {
                QueryWrapper<QuestionSubjectSelect> selectQueryWrapper = new QueryWrapper<QuestionSubjectSelect>()
                        .eq("question_id", question.getId())
                        .eq("subject_id", subject.getId());
                List<QuestionSubjectSelect> subjectSelectList = questionSubjectSelectService.list(selectQueryWrapper);
                subject.setSelectList(subjectSelectList);
            }
            question.setSubjectList(subjectList);
        }
        return R.ok().put("question", question);
    }

    @PostMapping("/save")
    public R save(HttpServletRequest request, @RequestBody Question question){
        return questionService.saveQuestion(request, question);
    }

    @PutMapping(value = "/update")
    public R update(@RequestBody Question question){
        return questionService.updateQuestion(question);
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
        boolean flag = questionService.removeById(id);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
